<?php
namespace app\controllers;

use YII;
use app\controllers\BaseController;
use app\models\GameUser;
use app\models\Morning;
use app\models\MorningRecord;

/***
获取用户具体信息
如总分 起床排名 总积分排名
***/

class UserController extends BaseController
{
    public $enableCsrfValidation = false;
    
    //获取用户的具体信息
	public function actionUserdata(){
		if( Yii::$app->request->isPost ){
    	    $postData = Yii::$app->request->post();
            //获取前端的请求
            $session = $postData['session'];
            //判读用户的session
    	    $userdata = $this->judgeSession($session);
            if( !empty($userdata['code']) ){
                return BaseController::FailRespone($data);
                exit(0);
            }
            //变量赋于
    		$userid = $userdata['id'];
            $nickname = $userdata['nickname'];
            $avatarurl = $userdata['avatarUrl'];
            //获取用户的缓存信息
            $usersign = Yii::$app->redis->get('signid:' . $userid);
            if( isset($usersign) ){
                $userinfo = json_decode($usersign,true);
                $usersign = true;
            }else{
                $userinfo = $usersign;
                $usersign = false;
            }
            //获取用户起床签到的具体信息
    	    $user = Morning::data_by_id($userid);
            //连续签到
		    $onsign = $user['onsign'];
            if($onsign != 0 ){
                //判段用户是否昨天签到
                $yetsign = $this->yesterdayWakeUpSign($userid);
                if($yetsign == -1 && empty($usersign)){
                    $signdays = 0;
                    $update = array(
                        'userid' => $userid,
                        'onsign' => $onsign,
                    );
                    Morning::signdaysUpdate($update);
                }
            }
    	    //今日排行
    	    $time = Morning::getMorningTime($userid);
            if( $time == -1 ){
                $morningrank = 0;
            }else{
                $morningrank = Morning::getMorningRank($time['time'],$user['msectime']);
            }
            if($user['totalscore'] != 0){
               $scoreRank = Morning::getTotalScoreRank($user['totalscore'] ,$user['time'],$user['msectime']); 
            }else{
                $scoreRank = 0;
            }
            //确保排名正确
            $userinfo['getupRank'] = $morningrank;
            $userinfo['scoreRank'] = $scoreRank;
            $lastten = Morning::LastTenbydatatime();
            //四六级时间
            $englishtime = BaseController::EnglishTime();
            //传出的data
    	    $data = array(
                'nickname' => $nickname,  //昵称
                'usersign' => $usersign,  //判断是否签到了
                'userinfo' => $userinfo,  //签到具体信息
    	   	    'onsign' => $onsign,      //连续签到天数
    	   	    'morningrank' => $morningrank, //签到排名
		  	    'scoreRank' => $scoreRank, //分数排名
                'totalscore' => $user['totalscore'], //总分
                'lastten' => $lastten,    //最后10名的url
                'englishtime' => $englishtime,//四六级时间
	        );    		    
    		return BaseController::SuccessRespone($data);
    	}else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            return BaseController::FailRespone($data);   
        }
        exit(0);
	}
    
    //获取用户的签到记录
	public function actionSignrecord(){
		if( Yii::$app->request->isPost ){
    	    $postData = Yii::$app->request->post();
            $session = $postData['session'];
    	    $userdata = $this->judgeSession($session);
            if( !empty($userdata['code']) ){
                return BaseController::FailRespone($data);
                exit(0);
            }
    		$userid = $userdata['id'];
    	    $data = MorningRecord::getMorningRecord($userid);
    	    if( empty($data) ){
    	    	$data = "你还没有开始签到";
		    }
    		return BaseController::SuccessRespone($data);
        }else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            return BaseController::FailRespone($data);   
        }
        exit(0);
	}

    public function yesterdayWakeUpSign($userid){
        $yetsign = MorningRecord::yesterdaySignup($userid);
        if($yetsign){
            $result = 1;
        }else{
            $result = -1;
        }
        return $result;
    }

    
}
